<?php

namespace App\Manage;


use Illuminate\Support\Facades\Config;
use App\Service\Photo\Upload;

class UploadManage extends Manage
{

    // 上传文件 本地上传
    public function local($myUser, $options = [])
    {
        $opt  = $this->option();
        $_is  = $opt->getData('is_compress', 0, 'intval');
        $file = $opt->request()->file('file');

        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        // 执行上传
        $upload = new Upload();
        $src    = $upload->local($file, $_is);
        if($src === false){
            return $this->_returnFail($upload->getError());
        }

        // 组合域名地址
        $domain = $opt->domain('oss');
        $src    = $domain.$src;


        return ['url' => $src];
    }

    // 图片信息（测试）
    public function thinkImage($myUser, $options = [])
    {
        // 执行上传
        $upload = new Upload();
        $src    = $upload->thinkImage();
        if($src === false){
            return $this->_returnFail($upload->getError());
        }
    }

    // 上传七牛云（并压缩图片在上传）
    public function qiniu($myUser, $options = [])
    {
        $opt  = $this->option();
        $_is  = $opt->getData('is_compress', 0, 'intval');
        $file = $opt->request()->file('file');

        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        // 执行上传
        $upload = new Upload();
        $src    = $upload->qiniu($file, $_is);
        if($src === false){
            return $this->_returnFail($upload->getError());
        }


        // 组合域名地址
        $domain = Config::get('site.cdnHost');
        if(substr($domain, -1) != '/'){
            $domain = $domain.'/';
        }
        $to = $domain.$src;

        return $to;
    }

    // 上传七牛云
    public function qiniu2($myUser, $options = [])
    {
        $opt  = $this->option();
        $_is  = $opt->getData('is_compress', 0, 'intval');
        $file = $opt->request()->file('file');

        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        // 执行上传
        $upload = new Upload();
        $_url   = $upload->qiniu2($file, $_is);
        if($_url === false){
            return $this->_returnFail($upload->getError());
        }


        return $_url;
    }

}


